const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//获取应用实例
const app = getApp();

Page({
  data: {
    shopList:[
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'1'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'2'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'3'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'4'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'5'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'6'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'7'},
      {image:"/static/images/1.jpg",name:'光明纯鲜奶',pic:'2.5',type:'0',id:'8'}
    ]
  },
  productFn(e){
    var id = e.currentTarget.dataset.item.id;
    console.log(id);
    // data中获取列表   
    var missionArr = this.data.shopList;
    missionArr.forEach((element,i) => {
      if (element.id === id) {
        missionArr[i].type = missionArr[i].type==='0'?"1":"0"
      }
    });
    // 数组重新赋值
    this.setData({
      shopList: missionArr
    })
  },
  // 确认订单
  exitLogin(){
    var data = []
    this.data.shopList.forEach((element,i) => {
      if (element.type === '1') {
        data.push(element)
      }
    });
    console.log(data)
    if(data == ''){
      wx.showToast({title: '请选择一种产品',icon: 'loading',duration: 1000,mask:true})
    }else{
      wx.removeStorage({
        key: 'shopArray',
        success: function(res) {},
      })
      wx.setStorageSync("shopArray",data)
      wx.navigateTo({
        url: '/pages/confirmProducts/confirmProducts'
      });
    }
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getIndexData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onLoad: function(options) {
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  
})